import Vue from "vue";
import VueRouter from "vue-router";
import CommodityDisplayVue from "@/components/front/CommodityDisplay.vue";
import AnimeMain from '@/components/front/AnimeMain.vue'
import Main from '@/Main.vue'
import testVue from "@/components/test/test.vue"
import test3Vue from "@/components/test/test3.vue"
import LoginVue from "@/components/front/Login.vue"
import SignVue from '@/components/front/Sign.vue'
import LoginEndVue from '@/components/rear-end/Login.vue'
import IndexVue from '@/components/rear-end/Index.vue'
import AdminVue from '@/components/rear-end/user/Admin.vue'
import CateVue from '@/components/rear-end/goods/Cate.vue'
import ParamsVue from "@/components/rear-end/goods/Params.vue"
import GoodsList from '@/components/rear-end/goods/List.vue'
import UploadVue from '@/components/rear-end/goods/Upload.vue'
import Add from '@/components/rear-end/goods/Add.vue'
import MineVue from '@/components/front/Mine.vue'
import MeVue from '@/components/front/me/Me.vue'
import AddressListVue from "@/components/front/me/AddressList.vue"
import ShoppingCatVue from '@/components/front/me/ShoppingCat.vue'
import AlreadyGoodsVue from "@/components/front/me/AlreadyGoods.vue"
import OrderList from "@/components/rear-end/order/OrderList.vue"
import StatisticsVue from '@/components/rear-end/Statistics.vue'
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/', component: Main,
      children: [
        { path: "", name: 'main', component: AnimeMain
      },
        { path: '/app1/:id', name: 'main', component: CommodityDisplayVue },
      ]
    },
    { path: '/test', component: testVue },
    {
      path: '/mine', component: MineVue,
      children: [
        { path: '', name: 'mine', component: MeVue },
        { path: '/me', name: 'mine', component: MeVue },
        { path: 'addressList', name: 'mine', component: AddressListVue },
        { path: 'shoppingCart', name: 'mine', component: ShoppingCatVue },
        { path: 'orders', name: 'mine', component: AlreadyGoodsVue }
      ]

    },
    { path: '/login', component: LoginVue },
    { path: '/sign', component: SignVue },
    { path: '/test3', component: test3Vue },
    {
      path: '/rear-end', component: LoginEndVue,
      meta: {
        isLogin: true,
      }
    },
    {
      path: '/rear-end/index', component: IndexVue,
      meta: {
        isLogin: true,
      },
      children: [
        { path: '', component: testVue },
        { path: '/user', component: AdminVue },
        { path: '/categories', component: CateVue },
        { path: '/params', component: ParamsVue },
        { path: '/goods', component: GoodsList },
        { path: '/upload', component: UploadVue },
        { path: '/goods/add', component: Add },
        { path:'/orders',component:OrderList},
        {path:'/statistics',component:StatisticsVue}
      ]
    }
  ]
}
)
// //挂载路由导航守卫
router.beforeEach((to,from,next)=>{
//to将要访问路径
//from代表从哪个路径跳转而来
// next是一个函数，表示放行
// next()放行 next('/login')强制跳转
if(to.matched.some(record=>record.meta.isLogin)){
  if(to.path==='/rear-end')return next()
  // 获取token
  // const tokenStr=window.sessionStorage.getItem('token')
  const tokenStr= localStorage.getItem("token")
  if(!tokenStr)return next('/rear-end')
  next()
}
else{
  next()
}
})
export default router